package com.bosicloud.cmb.ibp.account.controller;

import com.bosicloud.cmb.ibp.account.common.constant.UrlDefine;
import com.bosicloud.cmb.ibp.account.common.dto.LoginParamDTO;
import com.bosicloud.cmb.ibp.account.common.model.IbpUser;
import com.bosicloud.cmb.ibp.account.common.model.IbpUserApiKey;
import com.bosicloud.cmb.ibp.account.service.AccountManagerService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;

import static java.util.Objects.requireNonNull;

/**
 * 账号控制器
 *
 * @author Jack Peng / ziliang.peng@bosicloud.com
 */
@Slf4j
@RestController
@Api(value = "账号Controller", description = "账号管理")
public class AccountController {

    @Autowired
    private AccountManagerService accountManagerService;

    @ApiOperation(value = "登录", response = IbpUser.class, produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(UrlDefine.ACC_LOGIN)
    @HystrixCommand
    public IbpUser login(@ApiParam(name = "loginParamDTO", value = "登录参数", required = true)
                         @RequestBody @Valid LoginParamDTO loginParamDTO) {

        requireNonNull(loginParamDTO, "login parameters is required.");
        String md5DigestAsHex;
        try {
            md5DigestAsHex = DigestUtils.md5DigestAsHex(loginParamDTO.getPassword().getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException("password  unsupported encoding,");
        }
        return accountManagerService.login(loginParamDTO.getUsername(), md5DigestAsHex);
    }

    @ApiOperation(value = "创建用户ApiKey", response = Integer.class, produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = UrlDefine.CREATE_USER_API_KEY, produces = MediaType.APPLICATION_JSON_VALUE)
    @HystrixCommand
    public Integer createUserApiKey(@ApiParam(name = "ibpUserApiKey", value = "ibpUserApiKey", required = true)
                                    @RequestBody @Valid IbpUserApiKey ibpUserApiKey) {
        requireNonNull(ibpUserApiKey, "apiKeyVo parameters is required.");
        return accountManagerService.createUserApiKey(ibpUserApiKey);

    }

    @ApiOperation(value = "查询用户ApiKey信息", response = IbpUserApiKey.class, produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = UrlDefine.SEARCH_USER_API_KEY + "/users/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @HystrixCommand
    public List<IbpUserApiKey> searchUserApiKeyListByUserId(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @PathVariable(name = "userId", value = "userId") Integer userId) {
        return accountManagerService.searchUserApiKeyListByUserId(userId);

    }

}
